//
//  NSCalendar+OYExtension.m
//  xmcx
//
//  Created by 欧阳芳斌 on 16/8/3.
//  Copyright © 2016年 xmcx. All rights reserved.
//

#import "NSCalendar+XMExtension.h"

@implementation NSCalendar (XMExtension)

static NSCalendar *calendar_;

+ (void)load {
    
    if ([NSCalendar instancesRespondToSelector:@selector(calendarWithIdentifier:)]) {
        calendar_ = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    } else {
        calendar_ = [NSCalendar currentCalendar];
    }
}

+ (NSDateComponents *)currentDateComponents {
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar_ components:unitFlags fromDate:[NSDate date]];
}

+ (NSDateComponents *)componentsWithDate:(NSDate *)date {
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar_ components:unitFlags fromDate:date];
}

+ (NSInteger)currentMonth {
    
    return [NSCalendar currentDateComponents].month;
}

+ (NSInteger)currentYear {
    
    return [NSCalendar currentDateComponents].year;
}

+ (NSInteger)currentDay {
    
    return [NSCalendar currentDateComponents].day;
}

+ (NSInteger)currentWeekday {
    
    return [NSCalendar currentDateComponents].weekday;
}

+ (NSInteger)currentHour {
    
    return [NSCalendar currentDateComponents].hour;
}

+ (NSInteger)currentMinute {
    
    return [NSCalendar currentDateComponents].minute;
}

+ (NSInteger)currentSecond {
    
    return [NSCalendar currentDateComponents].second;
}

+ (NSInteger)getDaysWithYear:(NSInteger)year
                       month:(NSInteger)month {
    
    switch (month) {
        case 1:
            return 31;
            break;
        case 2:
            if (year%400 == 0 || (year%100 != 0 && year%4 == 0)) {
                return 29;
            }else{
                return 28;
            }
            break;
        case 3:
            return 31;
            break;
        case 4:
            return 30;
            break;
        case 5:
            return 31;
            break;
        case 6:
            return 30;
            break;
        case 7:
            return 31;
            break;
        case 8:
            return 31;
            break;
        case 9:
            return 30;
            break;
        case 10:
            return 31;
            break;
        case 11:
            return 30;
            break;
        case 12:
            return 31;
            break;
        default:
            return 0;
            break;
    }
}

+ (NSInteger)getFirstWeekdayWithYear:(NSInteger)year
                               month:(NSInteger)month {
    
    NSString *stringDate = [NSString stringWithFormat:@"%ld-%ld-01", (long)year, (long)month];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:stringDate];
    
    NSDateComponents *components = [calendar_ components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekday) fromDate:date];
    return [components weekday];
}

+ (NSComparisonResult)compareWithComponentsOne:(NSDateComponents *)componentsOne
                                 componentsTwo:(NSDateComponents *)componentsTwo {
    
    if (componentsOne.year == componentsTwo.year &&
        componentsOne.month == componentsTwo.month &&
        componentsOne.day   == componentsTwo.day) {
        return NSOrderedSame;
    }else if (componentsOne.year < componentsTwo.year ||
              (componentsOne.year == componentsTwo.year && componentsOne.month < componentsTwo.month) ||
              (componentsOne.year == componentsTwo.year && componentsOne.month == componentsTwo.month && componentsOne.day < componentsTwo.day)) {
        return NSOrderedAscending;
    }else {
        return NSOrderedDescending;
    }
}

+ (NSMutableArray *)arrayComponentsWithComponentsOne:(NSDateComponents *)componentsOne
                                       componentsTwo:(NSDateComponents *)componentsTwo {
    
    NSMutableArray *arrayComponents = [NSMutableArray array];
    
    NSString *stringOne = [NSString stringWithFormat:@"%zd-%zd-%zd", componentsOne.year,
                           componentsOne.month,
                           componentsOne.day];
    NSString *stringTwo = [NSString stringWithFormat:@"%zd-%zd-%zd", componentsTwo.year,
                           componentsTwo.month,
                           componentsTwo.day];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yy-MM-dd"];
    
    NSDate *dateFromString = [dateFormatter dateFromString:stringOne];
    NSDate *dateToString = [dateFormatter dateFromString:stringTwo];
    int timediff = [dateToString timeIntervalSince1970]-[dateFromString timeIntervalSince1970];
    
    NSTimeInterval timeInterval = [dateFromString timeIntervalSinceDate:dateFromString];
    
    for (int i = 0; i <= timediff; i+=86400) {
        timeInterval = i;
        NSDate *date = [dateFromString dateByAddingTimeInterval:timeInterval];
        
        NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
        [arrayComponents addObject:[calendar_ components:unitFlags fromDate:date]];
    }
    return arrayComponents;
}

+ (NSDateComponents *)dateComponentsWithString:(NSString *)string {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:string];
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return  [calendar_ components:unitFlags fromDate:date];
}

+ (NSDate *)dateFromComponents:(NSDateComponents *)dateComponents {
    
    return [calendar_ dateFromComponents:dateComponents];
}

+ (NSInteger)calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate {
    
    //取两个日期对象的时间间隔：
    //这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:typedef double NSTimeInterval;
    NSTimeInterval time = [endDate timeIntervalSinceDate:beginDate];
    
    int days = ((int)time) / (3600 * 24);
    
    return days;
}

@end
